package com.jinbo.footmessage.serverInf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.NavigationActivity;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.OrderBean;
import com.jinbo.footmessage.httpbean.ProjectBean;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.HttpProtocolFactory;
import com.jinbo.footmessage.httpretrofit.Listener;
import com.jinbo.footmessage.order.OrderActivity;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;
import com.jinbo.footmessage.utils.TimeUtils;
import com.jinbo.footmessage.utils.VerifyUtils;
import com.jinbo.footmessage.view.TitleBarView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ConfirmOrderActivity extends BaseActivity implements View.OnClickListener {
    private static String ORDER_INTENT = "order_bean";
    private static final int REQUEST_INFORMATION = 100;

    public static void launch(Activity activity, OrderBean orderBean) {
        Intent intent = new Intent(activity, ConfirmOrderActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ORDER_INTENT, orderBean);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

/*
    public static void launch(Activity activity, OrderBean orderBean,boolean isEdit) {
        Intent intent = new Intent(activity, ConfirmOrderActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("isEdit",false);
        bundle.putSerializable(ORDER_INTENT, orderBean);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }
*/

    TextView mDateTV, mWeekTV, mBeginTV, mEndTv, mProjectsTv, mTotalPriceTv, mNameTv;
    TextView mContactNameTv, mContactPhoneTv, mContactAddressTv, mContactSexTv;
    CircleImageView head;
    TextView mEditOrderInfTv;
    Button mSubmitBt;

    OrderBean bean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order);
        Intent intent = this.getIntent();
        bean = (OrderBean) intent.getSerializableExtra(ORDER_INTENT);
      //  boolean isEdit=intent.getBooleanExtra("isEdit",true);//默认是可以编辑
       // View editView=findViewById(R.id.edit_view);
        mSubmitBt = (Button) findViewById(R.id.submit);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        //if(isEdit)
            titleBarView.setCenterTitle("确认订单");
      /*  else {
            titleBarView.setCenterTitle("订单详情");
            editView.setVisibility(View.INVISIBLE);
            mSubmitBt.setVisibility(View.INVISIBLE);
        }*/
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);


        mEditOrderInfTv = (TextView) findViewById(R.id.edit_order_inf);
        mEditOrderInfTv.setOnClickListener(this);

        mSubmitBt.setOnClickListener(this);

        mDateTV = (TextView) findViewById(R.id.date);
        mWeekTV = (TextView) findViewById(R.id.day_of_week);
        mBeginTV = (TextView) findViewById(R.id.begin_time);
        mEndTv = (TextView) findViewById(R.id.end_time);
        mProjectsTv = (TextView) findViewById(R.id.projects);
        mTotalPriceTv = (TextView) findViewById(R.id.total_price);
        mNameTv = (TextView) findViewById(R.id.name);
        head = (CircleImageView) findViewById(R.id.head);
        mDateTV.setText(bean.date);
        mWeekTV.setText(bean.week);
        mBeginTV.setText(bean.begin_time);
        mEndTv.setText(bean.end_time);
        mProjectsTv.setText(bean.content);//getProjectNames(bean.worker.projects));
        mTotalPriceTv.setText(bean.total_price);//getPrice(bean.worker.projects) + "");
        Glide.with(this)
                .load(HttpProtocolFactory.HOST_URL_USER + bean.worker.head)
                .into(head);
        mNameTv.setText(bean.worker.name);

        mContactNameTv = (TextView) findViewById(R.id.contact_name);
        mContactPhoneTv = (TextView) findViewById(R.id.contact_phone);
        mContactAddressTv = (TextView) findViewById(R.id.contact_address);
        mContactSexTv = (TextView) findViewById(R.id.contact_sex);
        mContactNameTv.setText(MyApplication.local.getString(LocalPreferencesHelper.CONTACT_NAME));
        mContactPhoneTv.setText(MyApplication.local.getString(LocalPreferencesHelper.CONTACT_PHONE));
        mContactAddressTv.setText(MyApplication.local.getString(LocalPreferencesHelper.CONTACT_ADDRESS));
        mContactSexTv.setText(MyApplication.local.getString(LocalPreferencesHelper.CONTACT_SEX));
    }
/*
    String getProjectNames(List<ProjectBean> projects) {
        String s = "";
        for (int i = 0; i < projects.size(); i++) {
            if (projects.get(i).flag)
                s = s + projects.get(i).name + "/";
        }
        if (!s.equals("")) {
            int length = s.length();
            return s.substring(0, length - 1);
        } else
            return s;
    }

    private int getPrice(List<ProjectBean> mProjectsList) {
        int sum = 0;
        for (int i = 0; i < mProjectsList.size(); i++)
            if (mProjectsList.get(i).flag)
                sum = sum + Integer.valueOf(mProjectsList.get(i).price);
        return sum;
    }*/

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.edit_order_inf:
                String name, phone, address, sex;
                name = mContactNameTv.getText().toString();
                phone = mContactPhoneTv.getText().toString();
                address = mContactAddressTv.getText().toString();
                sex = mContactSexTv.getText().toString();
                EditOrderInfActivity.launch(this, name, phone, address, sex, REQUEST_INFORMATION);
                break;
            case R.id.submit:
                String user_id=MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
                String sexStr=mContactSexTv.getText().toString();
                String sexFlag="1";
                if(sexStr.equals("女"))
                    sexFlag="2";
                else
                    sexFlag="1";
                //show(bean.worker.id+"!"+user_id);
                BusinessController.getInstance().MakeOrder(bean.date, bean.week, bean.begin_time, bean.end_time, bean.content,
                        bean.total_price, bean.worker.id, user_id, mContactNameTv.getText().toString(),
                        mContactAddressTv.getText().toString(), mContactPhoneTv.getText().toString(),
                        sexFlag, TimeUtils.getNowTimeFormat(),
                        new Listener<OrderBean>() {

                            @Override
                            public void onStart(Object... params) {
                                showProgressDialog("请稍后...");
                            }

                            @Override
                            public void onComplete(OrderBean result, Object... params) {
                                dismissProgressDialog();
                                show("订单预约成功");
                                OrderActivity.launch(ConfirmOrderActivity.this);
                            }

                            @Override
                            public void onFail(final String msg, Object... params) {
                                dismissProgressDialog();
                                if (TextUtils.isEmpty(msg)) {
                                } else {
                                    show(msg);
                                }
                            }
                        });


                //NavigationActivity.launch(WelcomeActivity.this);
                //finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_INFORMATION) {
                String name, phone, address, sex;
                name =  data.getStringExtra("name");
                phone = data.getStringExtra("phone");
                address =data.getStringExtra("address");
                sex =data.getStringExtra("sex");
                mContactNameTv.setText(name);
                mContactPhoneTv.setText(phone);
                mContactAddressTv.setText(address);
                mContactSexTv.setText(sex);
            }

        }
    }
}
